﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Xml;

namespace Plate
{
    class ImageAttributes
    {
        bool hasplateposition;
        public bool HasPlatePosition
        {
            get { return this.hasplateposition; }
            set { this.hasplateposition = value; }
        }
        Rectangle plateposition;
        public Rectangle PlatePosition
        {
            get { return this.plateposition; }
            set { this.plateposition = value; }
        }
         //Point GreedFlagPosition;
        public static ImageAttributes FromXml(string xml)
        {
            ImageAttributes attributes = new ImageAttributes();
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xml);
            XmlNode basenode = xmldoc.ChildNodes[0];
            System.Diagnostics.Debug.Assert(basenode.Name == "CarImage");
            XmlAttribute attribute = null;
            //XmlAttribute attribute = basenode.Attributes["HFlip"];
            //if (attribute != null)
            //    attributes.HFlip = Convert.ToBoolean(attribute.Value);
            attribute = basenode.Attributes["HasPlatePosition"];
            if (attribute != null)
                attributes.HasPlatePosition = Convert.ToBoolean(attribute.Value);

            attribute = basenode.Attributes["XY"];
            Rectangle rect = new Rectangle();
            if (attribute != null)
            {
                char[] separator = { ':' };
                string[] xy = attribute.Value.Split(separator);
                Point begin = new Point(Convert.ToInt32(xy[0]), Convert.ToInt32(xy[1]));
                rect.X = begin.X;
                rect.Y = begin.Y;
                attribute = basenode.Attributes["WH"];
                if (attribute != null)
                {
                    xy = attribute.Value.Split(separator);
                    rect.Width = Convert.ToInt32(xy[0]);
                    rect.Height = Convert.ToInt32(xy[1]);
                }
                attributes.PlatePosition = rect;
            }
            return attributes;
        }
        public string ToXml()
        {
            XmlDocument doc = new XmlDocument();
            System.IO.StringWriter textwriter = new System.IO.StringWriter();
            XmlTextWriter xmlwriter = new XmlTextWriter(textwriter);
            XmlElement element = doc.CreateElement("CarImage");
            doc.AppendChild(element);
            XmlAttribute attribute = null;

            attribute = doc.CreateAttribute("HasPlatePosition");
            attribute.Value = this.HasPlatePosition.ToString();
            element.Attributes.Append(attribute);

            attribute = doc.CreateAttribute("XY");
            attribute.Value = this.plateposition.X + ":" + this.plateposition.Y;
            element.Attributes.Append(attribute);

            attribute = doc.CreateAttribute("WH");
            attribute.Value = this.plateposition.Width + ":" + this.plateposition.Height;
            element.Attributes.Append(attribute);

            doc.WriteTo(xmlwriter);
            return textwriter.ToString();
        }
   }
}
